package com.stylefeng.guns.modular.system.controller;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.stylefeng.guns.core.base.controller.BaseController;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;

import com.stylefeng.guns.core.log.LogObjectHolder;

import org.springframework.web.bind.annotation.RequestParam;

import com.stylefeng.guns.common.constant.factory.PageFactory;
import com.stylefeng.guns.common.persistence.model.Outbound;
import com.stylefeng.guns.modular.system.dao.SkateDao;
import com.stylefeng.guns.modular.system.service.IOutboundService;

/**
 * 轮滑出库管理控制器
 *
 * @author fengshuonan
 * @Date 2021-01-02 04:12:15
 */
@Controller
@RequestMapping("/outbound")
public class OutboundController extends BaseController {

    private String PREFIX = "/system/outbound/";

    @Autowired
    private IOutboundService outboundService;
    
    @Autowired
    private SkateDao skateDao;

    /**
     * 跳转到轮滑出库管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "outbound.html";
    }

    /**
     * 跳转到添加轮滑出库管理
     */
    @RequestMapping("/outbound_add")
    public String outboundAdd(Model model) {
    	List<Map<String, Object>> skates = skateDao.selectAllGroupBySkate(null,null,null,null);//分组查询所有种类轮滑,判断入库剩余量
    	List<Map<String, Object>> skates2 = skateDao.selectAllGroupBySkate1(null,null,null,null);
    	
    	for (Map<String, Object> map : skates) {
			String name = map.get("name").toString();
			for (Map<String, Object> map2 : skates2) {
				if(map2.get("name").equals(name)){//同一款鞋子
					map.put("sum",Integer.valueOf(map.get("sum").toString())-Integer.valueOf(map2.get("sum").toString()));
				}
			}
		}
    	model.addAttribute("skates",skates);
        return PREFIX + "outbound_add.html";
    }

    /**
     * 跳转到修改轮滑出库管理
     */
    @RequestMapping("/outbound_update/{outboundId}")
    public String outboundUpdate(@PathVariable Integer outboundId, Model model) {
        Outbound outbound = outboundService.selectById(outboundId);
        List<Map<String, Object>> skates = skateDao.selectAllGroupBySkate(null,null,null,null);//分组查询所有种类轮滑,判断入库剩余量
    	List<Map<String, Object>> skates2 = skateDao.selectAllGroupBySkate1(null,null,null,null);
    	
    	for (Map<String, Object> map : skates) {
			String name = map.get("name").toString();
			for (Map<String, Object> map2 : skates2) {
				if(map2.get("name").equals(name)){//同一款鞋子
					map.put("sum",Integer.valueOf(map.get("sum").toString())-Integer.valueOf(map2.get("sum").toString()));
				}
			}
		}
    	
    	List<Map<String, Object>> skates1 = skateDao.selectAllGroupBySkate(outbound.getName(),outbound.getType(),outbound.getColor(),outbound.getLevel());//分组查询所有种类轮滑,判断入库剩余量
    	List<Map<String, Object>> skates21 = skateDao.selectAllGroupBySkate1(outbound.getName(),outbound.getType(),outbound.getColor(),outbound.getLevel());
    	Integer count = 0;
    	if(skates1!=null && skates1.size()>0){
    		Map<String, Object> map = skates1.get(0);
    		Integer inCount = Integer.valueOf(map.get("sum").toString());
    		Integer ountCount = 0;
    		if(skates21!=null && skates21.size()>0){
    			Map<String, Object> map2 = skates21.get(0);
    			ountCount = Integer.valueOf(map2.get("sum").toString());
    			count = inCount - ountCount;
    		}else{
    			count = inCount-0;
    		}
    	}else{
    		count = 0;
    	}
    	System.out.println(count);
    	model.addAttribute("count",count);
    	model.addAttribute("skates",skates);
        model.addAttribute("outbound",outbound);
        return PREFIX + "outbound_edit.html";
    }

    /**
     * 获取轮滑出库管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(required=false)String name, @RequestParam(required=false)Integer type,@RequestParam(required=false)String color,@RequestParam(required=false)Integer level) {
    	Page<Map<String,Object>> page = new PageFactory<Map<String,Object>>().defaultPage();
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    	List<Map<String, Object>> selectList = skateDao.selectOutboundByCondition(page,name, type,color,level);
		List<Map<String, Object>> result = Lists.newArrayList();
		for (Map<String,Object> skate : selectList) {
			Map<String, Object> temp = Maps.newHashMap();
			temp.put("id", skate.get("id"));
			temp.put("name", skate.get("name"));
			if (skate.get("type") != null) {
				if (Integer.valueOf(skate.get("type").toString()) == 1) {
					temp.put("type", "28~31码");
				}
				if (Integer.valueOf(skate.get("type").toString()) == 2) {
					temp.put("type", "32~35码");
				}
				if (Integer.valueOf(skate.get("type").toString()) == 3) {
					temp.put("type", "36~39码");
				}
			} else {
				temp.put("type", "");
			}
			temp.put("number", skate.get("number"));
			if (skate.get("level") != null) {
				if (Integer.valueOf(skate.get("level").toString()) == 1) {
					temp.put("level", "儿童鞋");
				}
				if (Integer.valueOf(skate.get("level").toString()) == 2) {
					temp.put("level", "成人鞋");
				}
			} else {
				temp.put("level", "");
			}
			temp.put("outTime", skate.get("outTime")!=null?sdf.format(skate.get("outTime")):"");
			temp.put("color", skate.get("color"));
			temp.put("remark", skate.get("remark"));
			temp.put("createTime", skate.get("createTime"));
			result.add(temp);
		}
		page.setRecords(result);
		System.out.println("总数"+page.getTotal());
        return super.packForBT(page);
    }

    /**
     * 新增轮滑出库管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Outbound outbound) {
        outboundService.insert(outbound);
        return super.SUCCESS_TIP;
    }

    /**
     * 删除轮滑出库管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Long outboundId) {
        outboundService.deleteById(outboundId);
        return SUCCESS_TIP;
    }

    /**
     * 修改轮滑出库管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Outbound outbound) {
        outboundService.updateById(outbound);
        return super.SUCCESS_TIP;
    }

    /**
     * 轮滑出库管理详情
     */
    @RequestMapping(value = "/detail/{outboundId}")
    @ResponseBody
    public Object detail(@PathVariable("outboundId") Integer outboundId) {
        return outboundService.selectById(outboundId);
    }
    
    /**
     * 获取监听库存量
     */
    @RequestMapping(value = "/inventory")
    @ResponseBody
    public Integer inventory(@RequestParam(required=false)String name,@RequestParam(required=false)Integer type ,
    		@RequestParam(required=false) String color,@RequestParam(required=false)Integer level) {
    	List<Map<String, Object>> skates = skateDao.selectAllGroupBySkate(name,type,color,level);//分组查询所有种类轮滑,判断入库剩余量
    	List<Map<String, Object>> skates2 = skateDao.selectAllGroupBySkate1(name,type,color,level);
    	System.out.println(skates.toString());
    	System.out.println(skates2.toString());
    	Integer count = 0;
    	if(skates!=null && skates.size()>0){
    		Map<String, Object> map = skates.get(0);
    		Integer inCount = Integer.valueOf(map.get("sum").toString());
    		Integer ountCount = 0;
    		if(skates2!=null && skates2.size()>0){
    			Map<String, Object> map2 = skates2.get(0);
    			ountCount = Integer.valueOf(map2.get("sum").toString());
    			count = inCount - ountCount;
    		}else{
    			count = inCount-0;
    		}
    	}else{
    		count = 0;
    	}
    	System.out.println(count);
        return count;
    }
    
    
}





















